﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.DataEntity;
using ERP.Manager.DataProvider;
using ERP.Manager.DataHelper;
using ERP.Manager.Core;
using System.Data;
using System.Data.SqlClient;

namespace ERP.Manager.SQLServerProvider
{
    public class MscallerinfoRepository : IMscallerinfoRepository
    {
        /// <summary>
        /// 查询公告信息的访问量的方法
        /// </summary>
        /// <returns></returns>
        public List<Mscallerinfo> allgetmscallerinfo(int pagecount,int messageid)
        {
            List<Mscallerinfo> list = new List<Mscallerinfo>();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_GETMSCALLERINFO, 
                new SqlParameter("@pagecount",pagecount),
                new SqlParameter("@messageid", messageid));
            try
            {
                while (read.Read())
                {
                    Mscallerinfo mscaller = new Mscallerinfo();
                    mscaller.mscallerid = int.Parse(read["mscallerid"].ToString());
                    mscaller.messageinfo.messageId = int.Parse(read["messageId"].ToString());
                    mscaller.userinfo.userId = read["userid"].ToString();
                    mscaller.userinfo.userName = read["username"].ToString();
                    mscaller.callertime = DateTime.Parse(read["callertime"].ToString());
                    list.Add(mscaller);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        /// <summary>
        /// 查询当前用户有没有查阅公告信息的方法
        /// </summary>
        /// <param name="messageid"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public List<Mscallerinfo> allgetmscallerinfomessageid(int messageid, string userid)
        {
            List<Mscallerinfo> list = new List<Mscallerinfo>();           
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_GETMSCALLERINFO_MESSAGEID,
                new SqlParameter("@messageid", messageid),
                new SqlParameter("@userid", userid));
            try
            {
                while (read.Read())
                {
                    Mscallerinfo mscaller = new Mscallerinfo();
                    mscaller.mscallerid = int.Parse(read["mscallerid"].ToString());
                    mscaller.messageinfo.messageId = int.Parse(read["messageId"].ToString());
                    mscaller.userinfo.userId = read["userid"].ToString();
                    mscaller.callertime = DateTime.Parse(read["callertime"].ToString());
                    list.Add(mscaller);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        /// <summary>
        /// 添加公告访问信息的方法
        /// </summary>
        /// <param name="messageid"></param>
        /// <param name="userid"></param>
        /// <returns></returns>
        public int addmscallerinfo(Mscallerinfo mscaller)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.ADD_MSCALLERINFO,
                 new SqlParameter("@messageid", mscaller.messageinfo.messageId),
                 new SqlParameter("@userid", mscaller.userinfo.userId),
                 new SqlParameter("@callertime", mscaller.callertime));
            return rowcount;

        }
        /// <summary>
        /// 查询当前公告信息访问人数的总量的方法
        /// </summary>
        /// <param name="messageid"></param>
        /// <returns></returns>
        public int allgetmscallerinfocount(int messageid)
        {
            var result = SQLHelper.ExecuteScalar(CommandType.Text, ERPSQL.ALL_GETMSCALLERINFO_COUNT,
            new SqlParameter("@messageid",messageid));
            return int.Parse(result.ToString());
        }
    }
}
